package com.jamal2362.apktool.task;

import android.content.Context;
import java.io.File;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.files.Refreshable;
import com.jamal2362.apktool.util.FileUtils;
import com.jamal2362.apktool.util.Settings;

public class ExtractTask extends AbstractTask
{
	private Context context;
	private String name;

	public ExtractTask(Context ctx, String name, Refreshable refresh)
	{
		super(ctx, refresh);
		this.context = ctx;
		this.name = name;
	}

	@Override
	protected int getTitle()
	{
		return R.string.extract;
	}

	@Override
	protected boolean process(File f)
	{
		info(R.string.extraction, f.getAbsolutePath());
		String dir = Settings.output_directory;
		if (dir == null)
		{
			warning(R.string.output_directory_not_set);
			return false;
		}
		try
		{
			File d =  new File(dir);
			if (!d.exists() && !d.mkdirs())
			{
				warning(R.string.output_directory_not_extsts, dir);
				return false;
			}
			if (!d.isDirectory())
			{
				warning(R.string.not_directory, dir);
				return false;
			}
			name = FileUtils.genNameApk(context, dir, name, "", 0);
			File out = new File(d, name);
			boolean success = FileUtils.copyFiles(f, out);
			if (success)
				setResult(new File(dir));
			return success;
		}
		catch (Exception e)
		{
			warning(R.string.errorf, e.getMessage());
			return false;
		}
	}

	@Override
	protected boolean shouldShowFinishDialog()
	{
		return true;
	}

	@Override
	protected void onPostExecute(Boolean result)
	{
		super.onPostExecute(result);
	}
}
